﻿using System;
using System.Collections.Generic;

using Microsoft.Extensions.DependencyInjection;

namespace Jack.RedisLib
{
    public static class RedisLibServiceCollectionExtentions
    {
        public static IServiceCollection AddRedisClient(this IServiceCollection services, Action<RedisOptions> configOptions)
        {
            if (configOptions is null)
            {
                throw new ArgumentNullException(nameof(configOptions));
            }
            services.Configure(configOptions);
            services.AddSingleton<IRedisClient, RedisClient>();
            services.AddSingleton<IRedisLock, RedisLock>();
            services.AddTransient<IMultiRedisLock, MultiRedisLock>();

            return services;
        }

        public static IServiceCollection AddRedisClientFactory(this IServiceCollection services, Dictionary<string, Action<RedisOptions>> configOptions)
        {
            if (configOptions is null)
            {
                throw new ArgumentNullException(nameof(configOptions));
            }

            foreach (var configOption in configOptions)
            {
                services.Configure(configOption.Key, configOption.Value);
            }
            services.AddSingleton<IRedisClientFactory, RedisClientFactory>();
            services.AddSingleton<IRedisLock, RedisLock>();
            services.AddTransient<IMultiRedisLock, MultiRedisLock>();

            return services;
        }
    }
}
